package demo.practice.leetcode;



public class P100 {
    //https://leetcode-cn.com/problems/same-tree/submissions/
    public boolean isSameTree(TreeNode p, TreeNode q) {

        if(q==null||p==null){
            return q==p;
        }


        if(p.val!=q.val)
            return false;

        if( (p.left!=null && q.left==null )|| (p.left==null && q.left!=null)   )
            return false;

        if(p.left!=null && q.left!=null ){
            if(!isSameTree(p.left,q.left))
                return false;
        }

        if( (p.right!=null && q.right==null )|| (p.right==null && q.right!=null)   )
            return false;

        if(p.right!=null && q.right!=null ){
            if(!isSameTree(p.right,q.right))
                return false;
        }

        return true;
    }


    public boolean isSameTree2(TreeNode p, TreeNode q) {
       if(p==null || q==null){
           return q==p;
       }
       if(p.val!=q.val)
            return false;

       if(!isSameTree2(p.right, q.right))
           return false;

       if(!isSameTree2(p.left, q.left))
           return false;
        return true;
    }
}
